package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.item.Pet;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWAnimationBox;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIPetMenu implements IUIWindows{
	private DWFrame m_menu_frame = null;
	private DWBackground m_menu_background = null;
	private DWImageBox m_menu_xingxiang = null;
	private Bitmap[] m_menu_bm = new Bitmap[4];
	private DWAnimationBox m_menu_ab = null;
	private DWListbox listBox = null;
	public static boolean flag = false;

	private DWTextbox m_text_next = null;
	private DWListbox m_listBox_next = null;
	
	private DWButton m_menu_button01 = null;
	public static DWButton m_menu_button02 = null;
	private Bitmap m_bm_anxia = null;
	private DWTextbox[] m_menu_text = new DWTextbox[14];
	
	private DWTextbox m_menu_level = null;
	private DWTextbox m_menu_exp = null;
	//bitmap
	private byte m_bm_xingxiang = 0;
	private byte m_bm_button = 1;
	private byte m_bm_background01 = 2;
	private byte m_bm_background02 = 3;
	//text
	private byte m_text_leixing = 0;
	private byte m_text_dengji = 1;
	private byte m_text_jingyan = 2;
	private byte m_text_liliang = 3;
	private byte m_text_leixingzhi = 4;
	private byte m_text_dengjizhi = 5;
	private byte m_text_jingyanzhi = 6;
	private byte m_text_liliangzhi = 7;
	private byte m_text_zhili = 8;
	private byte m_text_zhilizhi = 9;
	private byte m_text_jiqiao = 10;
	private byte m_text_jiqiaozhi = 11;
	private byte m_text_minjie = 12;
	private byte m_text_minjiezhi = 13;
	
	private boolean flag_menu = false;
//	private Actor m_actor = new Actor(UIPet.UIPETMENU_DONGHUA);
	
	

	private void petMenu(){
		if(DWGameManager.AdpaterType == 2){
			m_menu_frame = new DWFrame(600, 450);
		}else{
			m_menu_frame = new DWFrame(400, 300);
		}
		m_menu_frame.setClickClose(true);
		m_menu_frame.setNearAnchor(UIPet.m_pet_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 10);
		//背景
		m_menu_background = new DWBackground(m_menu_bm[m_bm_background01],m_menu_frame.getShowWidth(), m_menu_frame.getShowHeight());
		m_menu_background.setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
	
		//宠物形象
		m_menu_ab = new DWAnimationBox(UIPet.m_pet_actor);
		m_menu_ab.setAction(2);
		m_menu_ab.setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 50, 10);

		//按钮
		m_menu_button01 = new DWButton("改名", m_menu_bm[m_bm_button]);
		m_menu_button01.setDownImage(m_bm_anxia);
		if(DWGameManager.AdpaterType == 2){
			m_menu_button01.setNearAnchor(m_menu_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 170);
		}else{
			m_menu_button01.setNearAnchor(m_menu_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 120);
		}
		
		m_menu_button02 = new DWButton("", m_menu_bm[m_bm_button]);
		m_menu_button02.setDownImage(m_bm_anxia);
		for(int i = 0;i < UIPet.pets.length;i++){
			if(UIPet.pets[UIPet.index].isChuZhan == true){
				m_menu_button02.setName("收回");
				break;
			}else{
				m_menu_button02.setName("出战");
			}
		}
		if(DWGameManager.AdpaterType == 2){
			m_menu_button02.setNearAnchor(m_menu_button01, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 150, 0);
		}else{
			m_menu_button02.setNearAnchor(m_menu_button01, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 100, 0);
		}

		//文本
		m_menu_text[m_text_leixing] = new DWTextbox("类型:");
		m_menu_text[m_text_leixing].setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 200, 10);
		
		//赋值
		m_menu_text[m_text_leixingzhi] = new DWTextbox(UIPet.UIPETMENU_LEIXING);
		m_menu_text[m_text_leixingzhi].setNearAnchor(m_menu_text[m_text_leixing], Tools.LEFT_TOP, Tools.RIGHT_TOP, 0, 0);
		
		m_menu_exp = new DWTextbox("经验为："+UIPet.UIPETMENU_JINGYAN+"/"+UIPet.UIPETMENU_ZUIDAJINGYAN);
		m_menu_exp.setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 200, 40);
		m_menu_level = new DWTextbox("等级为："+UIPet.UIPETMENU_DENGJI);
		m_menu_level.setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 200, 70);
		
		listBox = new DWListbox(200, 120);
		listBox.setNearAnchor(m_menu_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 200, 100);
		for(int j = 0;j < UIPet.pets[UIPet.index].AttributeS.length;j++){
			int type = UIPet.pets[UIPet.index].AttributeS[j][0];
			DWListSubItem lsi = new DWListSubItem();
			DWTextbox textBox = new DWTextbox(Pet.ATTRIBUTE_NAME[type]+":"+UIPet.pets[UIPet.index].AttributeS[j][1]);
			if(type == 0){
				textBox.setText(Pet.ATTRIBUTE_NAME[type]+":"+UIPet.pets[UIPet.index].AttributeS[j][1]+"%");
			}
			lsi.addControls(textBox);
			textBox.setNearAnchor(lsi, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
			listBox.addSubItem(lsi);
		}
		
		m_text_next = new DWTextbox("下一等级:");
		m_text_next.setNearAnchor(m_menu_ab, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
		
		m_listBox_next = new DWListbox(200, 120);
		m_listBox_next.setNearAnchor(m_text_next, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
		
		for (int j = 0; j < UIPet.pets[UIPet.index].NextAttributeS.length; j++) {
			int type = UIPet.pets[UIPet.index].NextAttributeS[j][0];
			DWListSubItem lsi = new DWListSubItem();
			DWTextbox textBox = new DWTextbox(Pet.ATTRIBUTE_NAME[type]
					+":"+ UIPet.pets[UIPet.index].NextAttributeS[j][1]);
			if(type == 0){
				textBox.setText(Pet.ATTRIBUTE_NAME[type]
						+":"+ UIPet.pets[UIPet.index].NextAttributeS[j][1]+"%");
			}
			lsi.addControls(textBox);
			textBox.setNearAnchor(lsi, Tools.LEFT_TOP, Tools.LEFT_TOP,
					0, 0);
			m_listBox_next.addSubItem(lsi);

		}
		
		m_menu_frame.addControl(m_menu_background);
		m_menu_frame.addControl(m_menu_ab);
		m_menu_frame.addControl(m_menu_exp);
		m_menu_frame.addControl(m_menu_level);
		m_menu_frame.addControl(m_menu_button01);
		m_menu_frame.addControl(m_menu_button02);
		m_menu_frame.addControl(listBox);
		m_menu_frame.addControl(m_text_next);
		m_menu_frame.addControl(m_listBox_next);
		m_menu_frame.addControl(m_menu_text[m_text_leixing]);
		m_menu_frame.addControl(m_menu_text[m_text_leixingzhi]);
		DWControlsManager.getInstance().addControl(m_menu_frame);
		
		//改名按钮
		m_menu_button01.addListener(new DWListener() {
			
			public void OnDoubleClick() {
			}
			
			public void OnClick() {
				final DWMessageBox m_message = new DWMessageBox("改名提示", "你确定花费20元宝改名？");
				m_menu_frame.addControl(m_message);
				m_message.addListener(new DWListener() {
					
					public void OnDoubleClick() {
					}
					
					public void OnClick() {
						if(m_message.getResult() == DWMessageBox.SOFTKEY_OK){
							UIWindows.getInstance().m_petMenu.open((byte)1);
							m_menu_frame.removeControl(m_message);
						}else{
							m_menu_frame.removeControl(m_message);
						}
							
						
					}
				});
			}
		});
		//出战、收回
		m_menu_button02.addListener(new DWListener() {
			
			public void OnDoubleClick() {
			}
			
			public void OnClick() {
				
						if(UIPet.pets[UIPet.index].isChuZhan == true){
							Long a = UIPet.pets[UIPet.index].guid;
							DWGameManager.getInstance().getSendMessage().sendChongWuChuZhan((byte)0, a);
							UIWindows.getInstance().m_petMenu.close((byte)0);
					}else{		
						Long a = UIPet.pets[UIPet.index].guid;
						Tools.debugPrintln("--------now  chu zhan-----"+UIPet.index);
						DWGameManager.getInstance().getSendMessage().sendChongWuChuZhan((byte)1, a);
						UIWindows.getInstance().m_petMenu.close((byte)0);
	
					}
				}
			
		});
	}
	public void init() {
//		m_menu_bm[m_bm_xingxiang] = UIWindows.createImage(UIPet.UIPETMENU_DONGHUA);
		m_menu_bm[m_bm_button] = UIWindows.createImage("/img/newui/anniu_2.gnp");
		m_bm_anxia = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		m_bm_up = UIWindows.createImage("/img/newui/anniu_6.gnp");
		m_bm_down = UIWindows.createImage("/img/newui/anniu_6ax.gnp");
		m_menu_bm[m_bm_background01] = UIWindows.createImage("/img/newui/beibaobj_2.gnp");
//		m_menu_bm[m_bm_background02] = UIWindows.createImage("/img/newui/liaotianxbjk_2.gnp");
		
	}

	//宠物改名界面
	private DWFrame m_name_frame = null;
	private DWTitle m_name_title = null;
	private DWBackground m_name_background = null;
	private DWTextbox m_textBox_tishi = null;
	private DWInputBox m_name_inputBox = null;
	private DWTextbox m_textBox_money = null;
	private DWButton m_button_ok = null;
	private DWButton m_button_back = null;
	private Bitmap m_bm_up = null;
	private Bitmap m_bm_down = null;
	
	private void changeName(){
		m_name_frame = new DWFrame(DWGameManager.Screen_Width / 2, DWGameManager.Screen_Height / 2);
		m_name_title = new DWTitle("宠物改名", m_name_frame);
		
		m_name_background = new DWBackground(
				DWGameManager.Screen_Width / 2 - 20, DWGameManager.Screen_Height / 2 - m_name_title.getShowHeight() + 20);
		m_name_background.setFill(true);
		m_name_background.setNearAnchor(m_name_frame, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 10);
		m_name_frame.addControl(m_name_background);
		
		m_textBox_tishi = new DWTextbox("请输入宠物名称(6字符以为)");
		m_textBox_tishi.setNearAnchor(m_name_title, Tools.LEFT_TOP, Tools.LEFT_TOP, 100, 50);
		
		m_name_inputBox = new DWInputBox(200, m_textBox_tishi.getShowHeight(), m_name_frame);
		m_name_inputBox.setNearAnchor(m_textBox_tishi, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 50, 10);
		
		m_textBox_money = new DWTextbox("改名需要花费"+20+"元宝。");
		m_textBox_money.setNearAnchor(m_name_inputBox, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, -50, 10);
		
		m_button_ok = new DWButton("确定", m_bm_up);
		m_button_ok.setDownImage(m_bm_down);
		m_button_ok.addListener(m_name_ok);
		m_button_ok.setNearAnchor(m_name_frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, -10);
		
		m_button_back = new DWButton("取消", m_bm_up);
		m_button_back.setDownImage(m_bm_down);
		m_button_back.addListener(m_name_back);
		m_button_back.setNearAnchor(m_name_frame, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -10, -10);
		
		m_name_frame.addControl(m_name_background);
		m_name_frame.addControl(m_name_title);
		m_name_frame.addControl(m_textBox_tishi);
		m_name_frame.addControl(m_name_inputBox);
		m_name_frame.addControl(m_textBox_money);
		m_name_frame.addControl(m_button_back);
		m_name_frame.addControl(m_button_ok);
		
		DWControlsManager.getInstance().addControl(m_name_frame);
				
	}
	//名字输入框监听
//	private DWListener m_input_name = new DWListener() {
//		
//		public void OnDoubleClick() {
//		}
//		
//		public void OnClick() {
//			
//		}
//	};
	
	//确定改名
	private DWListener m_name_ok = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			if (m_name_inputBox.getText() == null
					|| m_name_inputBox.getText().toString().equals("")) {
				DWGameManager.getInstance().showToast("请输入一个昵称",
						Toast.LENGTH_LONG);
			}
			else {
				if (m_name_inputBox.getText().toString().length() > 6
						|| m_name_inputBox.getText().toString().length() < 2) {
					DWGameManager.getInstance().showToast("请输入2到6个字符",
							Toast.LENGTH_LONG);
				}else{
					//发送指令
					DWGameManager.getInstance().getSendMessage().sendChangePetName(UIPet.pets[UIPet.index].guid	, m_name_inputBox.getText().toString());
				}
			}
			UIWindows.getInstance().m_petMenu.close((byte)1);
		}
	};
	
	//取消改名
	private DWListener m_name_back = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_petMenu.close((byte)1);
		}
	};
	
	public void open(byte index) {
		if(index == 0){
			//if(flag_menu == false){
			petMenu();
		//	flag_menu = true;
//		}else{
//			return;
//		}
		}else if(index ==1){
			changeName();
		}
		
		
	}

	public void close(byte index) {
		if(index == 0){
//			flag = false;
			DWControlsManager.getInstance().removeControl(m_menu_frame);
		}else if(index == 1){
			DWControlsManager.getInstance().removeControl(m_name_frame);
		}

	}

	public void dispose() {
		
	}
	public void logic()
	{
		// TODO Auto-generated method stub
		
	}

	//判断是否在容器中
	public DWButton isNewButton(){
		if(DWControlsManager.getInstance().contains(m_menu_frame) == false){
			return null;
		}else
			return m_menu_button02;
	}
	/**
	 * 接收宠物改名
	 * @param dis
	 * @throws IOException
	 */
	public void recvChangeName(DataInputStream dis) throws IOException{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_宠物);
		try {
			byte state = dis.readByte();
			if(state == 0){
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_C,
						"元宝不足");
				return;
			}
			long id = dis.readLong();
			String name = dis.readUTF();
			UIPet.pets[UIPet.index].name = name;
			m_menu_text[m_text_leixingzhi].setText(name);
			if(UIPet.pets[UIPet.index].isChuZhan == true){
				UIWindows.getInstance().m_pet.m_pet_leixing.setText("名字"+"："+name);
			}
		} catch (Exception e) {
			Tools.debug(e);
		}
	}
	
}
